package org.films.web;

import java.lang.Long;
import java.lang.String;
import java.security.Principal;
import java.util.Date;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.films.domain.Actor;
import org.films.domain.Category;
import org.films.domain.Comment;
import org.films.domain.Consumer;
import org.films.domain.Director;
import org.films.domain.Film;
import org.films.domain.Study;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

privileged aspect FilmController_Roo_Controller {
    
    @RequestMapping(value = "/film", method = RequestMethod.POST)    
    public String FilmController.create(@Valid Film film, BindingResult result, ModelMap modelMap) {    
        if (film == null) throw new IllegalArgumentException("A film is required");        
        if (result.hasErrors()) {        
            modelMap.addAttribute("actors", Actor.findAllActors());            
            modelMap.addAttribute("categorys", Category.findAllCategorys());            
            modelMap.addAttribute("comments", Comment.findAllComments());            
            modelMap.addAttribute("directors", Director.findAllDirectors());            
            modelMap.addAttribute("studys", Study.findAllStudys());            
            modelMap.addAttribute("film_releaseYear_date_format", org.joda.time.format.DateTimeFormat.patternForStyle("S-", org.springframework.context.i18n.LocaleContextHolder.getLocale()));            
            return "film/create";            
        }        
        film.persist();        
        return "redirect:/film/" + film.getId();        
    }    
    
    @RequestMapping(value = "/film/form", method = RequestMethod.GET)    
    public String FilmController.createForm(ModelMap modelMap) {    
        modelMap.addAttribute("film", new Film());        
        modelMap.addAttribute("actors", Actor.findAllActors());        
        modelMap.addAttribute("categorys", Category.findAllCategorys());        
        modelMap.addAttribute("comments", Comment.findAllComments());        
        modelMap.addAttribute("directors", Director.findAllDirectors());        
        modelMap.addAttribute("studys", Study.findAllStudys());        
        modelMap.addAttribute("film_releaseYear_date_format", org.joda.time.format.DateTimeFormat.patternForStyle("S-", org.springframework.context.i18n.LocaleContextHolder.getLocale()));        
        return "film/create";        
    }    
    
    @RequestMapping(value = "/film/{id}", method = RequestMethod.GET)    
    public String FilmController.show(@PathVariable("id") Long id, ModelMap modelMap) {    
        if (id == null) throw new IllegalArgumentException("An Identifier is required");        
        modelMap.addAttribute("film_releaseYear_date_format", org.joda.time.format.DateTimeFormat.patternForStyle("S-", org.springframework.context.i18n.LocaleContextHolder.getLocale()));        
        modelMap.addAttribute("film", Film.findFilm(id));        
        return "film/show";        
    }    
    
    @RequestMapping(value = "/film", method = RequestMethod.GET)    
    public String FilmController.list(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, ModelMap modelMap) {    
        if (page != null || size != null) {        
            int sizeNo = size == null ? 10 : size.intValue();            
            modelMap.addAttribute("films", Film.findFilmEntries(page == null ? 0 : (page.intValue() - 1) * sizeNo, sizeNo));            
            float nrOfPages = (float) Film.countFilms() / sizeNo;            
            modelMap.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));            
        } else {        
            modelMap.addAttribute("films", Film.findAllFilms());            
        }        
        modelMap.addAttribute("film_releaseYear_date_format", org.joda.time.format.DateTimeFormat.patternForStyle("S-", org.springframework.context.i18n.LocaleContextHolder.getLocale()));        
        return "film/list";        
    }    
    
    @RequestMapping(method = RequestMethod.PUT)    
    public String FilmController.update(@Valid Film film, BindingResult result, ModelMap modelMap) {    
        if (film == null) throw new IllegalArgumentException("A film is required");        
        if (result.hasErrors()) {        
            modelMap.addAttribute("actors", Actor.findAllActors());            
            modelMap.addAttribute("categorys", Category.findAllCategorys());            
            modelMap.addAttribute("comments", Comment.findAllComments());            
            modelMap.addAttribute("directors", Director.findAllDirectors());            
            modelMap.addAttribute("studys", Study.findAllStudys());            
            modelMap.addAttribute("film_releaseYear_date_format", org.joda.time.format.DateTimeFormat.patternForStyle("S-", org.springframework.context.i18n.LocaleContextHolder.getLocale()));            
            return "film/update";            
        }        
        film.merge();        
        return "redirect:/film/" + film.getId();        
    }    
    
    @RequestMapping(value = "/film/{id}/form", method = RequestMethod.GET)    
    public String FilmController.updateForm(@PathVariable("id") Long id, ModelMap modelMap) {    
        if (id == null) throw new IllegalArgumentException("An Identifier is required");        
        modelMap.addAttribute("film", Film.findFilm(id));        
        modelMap.addAttribute("actors", Actor.findAllActors());        
        modelMap.addAttribute("categorys", Category.findAllCategorys());        
        modelMap.addAttribute("comments", Comment.findAllComments());        
        modelMap.addAttribute("directors", Director.findAllDirectors());        
        modelMap.addAttribute("studys", Study.findAllStudys());        
        modelMap.addAttribute("film_releaseYear_date_format", org.joda.time.format.DateTimeFormat.patternForStyle("S-", org.springframework.context.i18n.LocaleContextHolder.getLocale()));        
        return "film/update";        
    }    
    
    @RequestMapping(value = "/film/{id}", method = RequestMethod.DELETE)    
    public String FilmController.delete(@PathVariable("id") Long id, @RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size) {    
        if (id == null) throw new IllegalArgumentException("An Identifier is required");        
        Film.findFilm(id).remove();        
        return "redirect:/film?page=" + ((page == null) ? "1" : page.toString()) + "&size=" + ((size == null) ? "10" : size.toString());        
    }    
    
    @RequestMapping(value = "find/ByStudyAndReleaseYearEquals/form", method = RequestMethod.GET)    
    public String FilmController.findFilmsByStudyAndReleaseYearEqualsForm(ModelMap modelMap) {    
        modelMap.addAttribute("studys", Study.findAllStudys());        
        modelMap.addAttribute("film_releaseYear_date_format", org.joda.time.format.DateTimeFormat.patternForStyle("S-", org.springframework.context.i18n.LocaleContextHolder.getLocale()));        
        return "film/findFilmsByStudyAndReleaseYearEquals";        
    }    
    
    @RequestMapping(value = "find/ByStudyAndReleaseYearEquals", method = RequestMethod.GET)    
    public String FilmController.findFilmsByStudyAndReleaseYearEquals(@RequestParam("study") Study study, @RequestParam("releaseyear") @DateTimeFormat(style = "S-") Date releaseYear, ModelMap modelMap) {    
        if (study == null) throw new IllegalArgumentException("A Study is required.");        
        if (releaseYear == null) throw new IllegalArgumentException("A ReleaseYear is required.");        
        modelMap.addAttribute("films", Film.findFilmsByStudyAndReleaseYearEquals(study, releaseYear).getResultList());        
        modelMap.addAttribute("film_releaseYear_date_format", org.joda.time.format.DateTimeFormat.patternForStyle("S-", org.springframework.context.i18n.LocaleContextHolder.getLocale()));        
        return "film/list";        
    }    
    
    @RequestMapping(value = "find/ByActors/form", method = RequestMethod.GET)    
    public String FilmController.findFilmsByActorsForm(ModelMap modelMap) {    
        modelMap.addAttribute("actors", Actor.findAllActors());        
        return "film/findFilmsByActors";        
    }    
    
    @RequestMapping(value = "find/ByActors", method = RequestMethod.GET)    
    public String FilmController.findFilmsByActors(@RequestParam("actors") Set<Actor> actors, ModelMap modelMap) {    
        if (actors == null) throw new IllegalArgumentException("A Actors is required.");        
        modelMap.addAttribute("films", Film.findFilmsByActors(actors).getResultList());        
        return "film/list";        
    }    
    
    @RequestMapping(value = "/film/action/leave_comment", method = RequestMethod.POST)
    public String FilmController.addComment(HttpServletRequest request, ModelMap modelMap) {    	
    	Long id = Long.parseLong(request.getParameter("filmId"));
    	String body = request.getParameter("comment");
    	
    	if (id == null) {
    		throw new IllegalArgumentException("An Identifier is required");
    	}
    	    	
    	Film film = Film.findFilm(id);
    	Principal principal = request.getUserPrincipal();
    	Consumer consumer = Consumer.findConsumerByName(principal.getName());
    	
    	createComment(body, consumer, film).persist();
    	
    	modelMap.addAttribute("film", film);
    	return "film/show";
    }
    
    
    private static Comment createComment(String body, Consumer consumer, Film film) {
    	Comment result = new Comment();
    	result.setBody(body);
    	result.setConsumer(consumer);
    	result.setDateAdd(new Date());
    	result.setFilm(film);
    	return result;
    }
    
}
